package com.example.ormsqlsplicingframe1.orm.wrapper;

import java.util.*;

public class UpdateWrapper {

    private Title title = new Title();

    private Where where = new Where();

    private List<Object> values = null;

    //等于
    private Object eq;

    //不等于
    private Object notEq;

    //多个
    private List<Object> inList = null;

    public Title title(){
        return title;
    }

    private Map<String,Object> map = new HashMap<>();

    public UpdateWrapper updateColumn(String column, Object value){
        map.put(column, value);
        return this;

    }

    public List<Object> getValues(){
        return values;
    }

    public UpdateWrapper eq(Object value){

        eq = value;

        return this;
    }

    public UpdateWrapper not(Object value){

        notEq = value;
        return this;
    }

    public UpdateWrapper in(Object ...value){

        inList = new ArrayList<>(value.length);
        for (Object o : value) {
            inList.add(o);
        }
        return this;
    }


    private String getSql(){

        StringBuilder builder = new StringBuilder();

        if (title.getSql() != null && !title.getSql().equals("")){
            builder.append(" UPDATE "+title.getSql());
        }

        if (map.size() > 0){

            values = new ArrayList();
            StringJoiner joiner = new StringJoiner(",");

            map.keySet().forEach(row -> {
                Object value = map.get(row);
                values.add(value);
                joiner.add(row +" = ?");
            });

            builder.append(joiner);
        }



        return builder.toString();

    }
}
